combination object

This method fills an array with the next combination in the sequence generated by the currently used algorithm.

bool next(int[]@ list)

Parameters:
list
An array of ints which will be automatically resized if necessary, and then filled with the next combination in the sequence.

Return value:
true if the array was successfully filled with the next combination, false if there are no more combinations or if an error occurred.

Remarks:
When calling this method, you should always check the return value. if true is returned, it means that your array is now populated with the next combination in the sequence as dictated by the algorithm that you are using. If false is returned and no error is set, it means that all combinations were enumerated successfully.

The reason this method takes the array as an argument rather than returning it is to avoid the necessity of making a new array instance each time. Since this method has the potential of being called many times, it is important to avoid as much unnecessary processing as possible (such as making a new array which requires memory allocations).

Example:
// We have a basket and an unlimited quantity of three types of fruit. The basket may only contain two fruits at any given time.
// Enumerate all the combinations of fruit that the basket could possibly contain.
// Note: This same algorithm can be used for other things such as getting all the possible outcomes of rock, paper and sizzors.

void main()
{
int combination_size=2;
// This is the number of items that we want in each combination.

int items=3;
// This is the number of items we have all in total, which is to say our different types of fruit.

combination process;

if(process.generate_all_combinations(items, combination_size)==false)
{
alert("Error", get_last_error_text());
exit();
}

int[] list;
// This is the array which will be filled with each combination.

int count=0;
// This simply keeps track of how many combinations we've generated so far. It is not really needed.

while(process.next(list))
{
count+=1;

// At this point, we have a new combination. We print it out in an alert box as a comma separated string.
string data;
for(int i=0;i<list.length();i++)
{
if(i>0)
data+=", ";
data+=list[i];
}
alert("Combination " + count, data);
}

// We're done, so let's display how many combinations we found before we quit.
alert("Result", count + " combinations were generated from a set of " + items + " items, with " + combination_size + " items in each combination.");
}